﻿using System;
using System.Collections.Generic;
using System.Web;
using DotNetNuke.Web.Mvp;
using DotNetNuke.Services.Exceptions;
using System.Collections;

namespace YourCompany.Modules.$safeprojectname$
{
    public class View$safeprojectname$Presenter : ModulePresenter<IView$safeprojectname$, View$safeprojectname$Model>
    {
        #region "Private Members"
        private I$safeprojectname$Controller controller;
        private Dictionary<string, string> settings;
        #endregion

        #region "Constructors"
        /// <summary>
        /// Initializes a new instance of the <see cref="View$safeprojectname$Presenter"/> class.
        /// </summary>
        /// <param name="view">The view.</param>
        public View$safeprojectname$Presenter(IView$safeprojectname$ view)
            : this(view, new $safeprojectname$Controller(DataProvider.Instance()))
        {

        }

        /// <summary>
        /// Initializes a new instance of the <see cref="View$safeprojectname$Presenter"/> class.
        /// </summary>
        /// <param name="view">The view.</param>
        /// <param name="controller">The controller.</param>
        public View$safeprojectname$Presenter(IView$safeprojectname$ view, I$safeprojectname$Controller controller)
            : base(view)
        {
            if (controller == null)
                throw new ArgumentNullException("controller", "controller is null.");
            this.controller = controller;
        }
        #endregion

        #region "Propertites"
        /// <summary>
        /// Gets the controller.
        /// </summary>
        public I$safeprojectname$Controller Controller
        {
            get { return controller; }
        }

        /// <summary>
        /// Gets or sets the settings.
        /// </summary>
        /// <value>
        /// The settings.
        /// </value>
        public Dictionary<string, string> Settings
        {
            get { return settings; }
            set { settings = value; }
        }

        /// <summary>
        /// Gets the formatting template.
        /// </summary>
        private string FormattingTemplate
        {
            get
            {
                var formattedString = string.Empty;
                if (!string.IsNullOrEmpty((string)Settings["template"]))
                {
                    formattedString = (string)Settings["template"];
                }
                return formattedString;
            }
        }
        #endregion

        #region "Base Class Overrides"
        /// <summary>
        /// Called when [init].
        /// </summary>
        protected override void OnInit()
        {
            base.OnInit();
            try
            {
                View.Model.IsEditable = IsEditable;

                List<$safeprojectname$Info> obj$safeprojectname$Infos;

                // get the content from the $safeprojectname$ table
                obj$safeprojectname$Infos = Controller.Get$safeprojectname$s(ModuleId);

                if (obj$safeprojectname$Infos.Count == 0)
                {
                    //add the content to the $safeprojectname$ table
                    var obj$safeprojectname$Info = new $safeprojectname$Info();
                    obj$safeprojectname$Info.ModuleId = ModuleId;
                    obj$safeprojectname$Info.Content = "View.Content";
                    obj$safeprojectname$Info.CreatedByUser = UserId;
                    obj$safeprojectname$Info.ItemId = Controller.Add$safeprojectname$(obj$safeprojectname$Info);
                    obj$safeprojectname$Infos.Add(obj$safeprojectname$Info);
                }
                foreach (var obj in obj$safeprojectname$Infos)
                {
                    if (string.IsNullOrEmpty(FormattingTemplate.Trim()))
                    {
                        obj.DisplayContent = obj.Content;
                    }
                    else
                    {
                        obj.DisplayContent = FormatContent(obj);
                    }
                }
            }
            catch (Exception exc)
            {
                // 'Show a UI message later
                Exceptions.LogException(exc);
            }
        }

        /// <summary>
        /// Loads from context.
        /// </summary>
        protected override void LoadFromContext()
        {
            base.LoadFromContext();
            if (ModuleContext != null)
                settings = new Dictionary<string, string>();
            foreach (DictionaryEntry item in ModuleContext.Settings)
            {
                settings.Add(item.Key.ToString(), item.Value.ToString());
            }
        }
        #endregion

        #region "Private Methods"
        /// <summary>
        /// Formats the content.
        /// </summary>
        /// <param name="info">The info.</param>
        /// <returns></returns>
        private string FormatContent($safeprojectname$Info info)
        {
            var formatString = FormattingTemplate;
            formatString = formatString.Replace("[MODULEID]", info.ModuleId.ToString());
            formatString = formatString.Replace("[ITEMID]", info.ItemId.ToString());
            formatString = formatString.Replace("[CONTENT]", info.Content);
            formatString = formatString.Replace("[CREATEDBYUSER]", info.CreatedByUser.ToString());
            formatString = formatString.Replace("[CREATEDBYUSERNAME]", info.CreatedByUserName);
            formatString = formatString.Replace("[CREATEDDATE]", info.CreatedDate.ToString());
            return formatString;
        }
        #endregion
    }
}